<template>
  <div class="page-content">
    <cy-table
      :listData="dataList"
      :listCount="dataCount"
      v-bind="contentTableConfig"
      v-model:page="pageInfo"
    >
      <!-- 1.1.1 具名插槽--table外部的数据 header中的插槽 -->
      <!-- E步   通过v-if判断，管理按钮权限，有权限才会展示button，否则不展示 -->
      <template #headerHandler>
        <el-button
          v-if="isCreate"
          type="primary"
          size="medium"
          @click="handleCreateClick"
          >新建用户
        </el-button>
      </template>

      <!-- 1.1.2 作用域插槽的使用--列里面的数据 列中的插槽-->
      <template #status="scope">
        <el-button plain :type="scope.row.enable ? 'success' : 'danger'">
          {{ scope.row.enable ? '启用' : '禁用' }}
        </el-button>
      </template>

      <template #createAt="scope">
        {{ $filters.formatTime(scope.row.createAt) }}
      </template>
      <template #updateAt="scope">
        {{ $filters.formatTime(scope.row.update) }}
      </template>

      <template #handler="scope">
        <div class="handle-btns">
          <el-button
            v-if="isUpdate"
            size="small"
            type="text"
            @click="handleEditClick(scope.row)"
            >编辑
          </el-button>
          <el-button
            v-if="isDelete"
            size="small"
            type="text"
            @click="handleDeleteClick(scope.row)"
            >删除
          </el-button>
        </div>
      </template>

      <!-- （二）、在page-content中动态的插入插槽，但是数据是从goods页面传进来的，需要排除固定的插槽 -->
      <template
        v-for="item in otherPropSlots"
        :key="item.prop"
        #[item.slotName]="scope"
      >
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </cy-table>
  </div>
</template>

<script lang="ts">
import cyTable from '@/base-ui/table'
import { defineComponent, computed, ref, watch } from 'vue'
import { usePermission } from '@/hooks/use-permission'

import { useStore } from '@/store'

export default defineComponent({
  props: {
    contentTableConfig: {
      type: Object,
      required: true
    },
    pageName: {
      type: String,
      required: true
    }
  },
  components: {
    cyTable
  },
  emits: ['createBtnClick', 'editBtnClick'],
  setup(props, { emit }) {
    const store = useStore()
    //D步 ---- 0. 调用hook ， 进而可以获取操作的权限
    const isCreate = usePermission(props.pageName, 'create')
    const isUpdate = usePermission(props.pageName, 'update')
    const isDelete = usePermission(props.pageName, 'delete')
    const isQuery = usePermission(props.pageName, 'query')

    //1. 页面展示数据双向绑定
    const pageInfo = ref({ currentPage: 1, pageSize: 10 })
    //2. watch 监听pageInfo的改变,重新发送网络请求并且携带参数。参数也更改为响应式的数据
    watch(pageInfo, () => {
      getPageData()
    })
    //3. 发送网络请求
    const getPageData = (queryInfo: any = {}) => {
      //如果没有查询的权限，则不发送网络请求
      if (!isQuery) return
      store.dispatch('systemModule/getPageListAction', {
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...queryInfo
        }
      })
    }
    getPageData()
    // 4. 从vuex中获取数据
    const dataList = computed(() =>
      store.getters[`systemModule/pageListData`](props.pageName)
    )
    const dataCount = computed(() =>
      store.getters[`systemModule/pageCountData`](props.pageName)
    )
    // 5.（一）、 获取其他的动态插槽名称,exclude stable slots
    const otherPropSlots = props.contentTableConfig?.propList.filter(
      (item: any) => {
        if (item.slotName === 'status') return false
        if (item.slotName === 'createAt') return false
        if (item.slotName === 'updateAt') return false
        if (item.slotName === 'handler') return false
        return true
      }
    )
    //6. delete/edit/create 等操作
    const handleDeleteClick = (item: any) => {
      store.dispatch('systemModule/deletePageDataAction', {
        pageName: props.pageName,
        id: item.id
      })
    }
    const handleCreateClick = () => {
      emit('createBtnClick')
    }
    const handleEditClick = (item: any) => {
      emit('editBtnClick', item)
    }
    return {
      dataList,
      dataCount,
      getPageData,
      pageInfo,
      otherPropSlots,
      isCreate,
      isDelete,
      isUpdate,
      handleDeleteClick,
      handleCreateClick,
      handleEditClick
    }
  }
})
</script>

<style scoped>
.content {
  padding: 20px;
  border-top: 20px solid #f5f5f5;
}
</style>
